package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.authoriseRFC;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.HistoryMaker;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.Workorder;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.authorizeRFCPanel.authorizeRFCPanelSubs.CreateWorkOrderDialog;

public class GenerateWorkorderThread extends Thread {

	private CreateWorkOrderDialog workOrderDialog;
	private DatabaseConnection connection;

	public GenerateWorkorderThread(CreateWorkOrderDialog workOrderDialog) {
		this.workOrderDialog = workOrderDialog;
	}

	@Override
	public void run() {
		this.connection = new DatabaseConnection();
		this.connection.connect();
		this.workOrderDialog.getProgress().setVisible(true);
		this.workOrderDialog.getCreateWorkorder().setEnabled(false);

		this.updateWO();

		this.updateRFC();

		// *******History ************

		new HistoryMaker().addHistoryInDBTable(workOrderDialog
				.getAuthorizeRFCPanel().getMainframe().getSignedInUser()
				.getFullName(), this.workOrderDialog.getRfc().getIdRFC(),
				"RFC wurde authorisiert und entsprechende Workorder erstellt");

		//*************
		
		new LoadreadyforApprovalRFCThread(
				workOrderDialog.getAuthorizeRFCPanel()).start();
		this.workOrderDialog.getProgress().setVisible(false);
		this.connection.disconnect();
		this.workOrderDialog.dispose();

	}

	private void updateRFC() {

		String query = "UPDATE `cms_rfc` SET `" + RFC.TC_Status
				+ "` = 'approved' WHERE `" + RFC.TC_ID + "` = '"
				+ this.workOrderDialog.getRfc().getIdRFC() + "'";

		try {

			this.connection.insertquery(query);

		} catch (Exception e) {
			System.out.println("RFC error");
		}

	}

	private boolean updateWO() {

		String query = "INSERT INTO `"
				+ Workorder.WORKORDER_TABLE
				+ "` (`"
				+ Workorder.TC_WO_ID
				+ "`, `"
				+ Workorder.TC_RFC_ID
				+ "`, `"
				+ Workorder.TC_WO_TITLE
				+ "`, `"
				+ Workorder.TC_WO_TYP
				+ "`, `"
				+ Workorder.TC_RESPONIBLE
				+ "`, `"
				+ Workorder.TC_AFFECTED_SERVICES
				+ "`, `"
				+ Workorder.TC_AFFECTED_INFRASTRUCTURES
				+ "`, `"
				+ Workorder.TC_PLANNED_START
				+ "`, `"
				+ Workorder.TC_PLANNED_END
				+ "`, `"
				+ Workorder.TC_PLANNED_EFFORD
				+ "`, `"
				+ Workorder.TC_DESCRIPTION
				+ "`, `"
				+ Workorder.TC_WO_STATUS
				+ "`, `"
				+ Workorder.TC_NOTES
				+ "`) VALUES ('"
				+ this.workOrderDialog.getWoID().getText()
				+ "', '"
				+ this.workOrderDialog.getRfc().getIdRFC()
				+ "', '"
				+ this.workOrderDialog.getWoTitle().getText()
				+ "', '"
				+ this.workOrderDialog.getWoType().getSelectedItem().toString()
				+ "', '"
				+ this.workOrderDialog.getWoResponsible().getSelectedItem()
						.toString() + "', '"
				+ this.workOrderDialog.getWoAffectedServices().getText()
				+ "', '"
				+ this.workOrderDialog.getWoAffectedInfrastructure().getText()
				+ "', '" + this.workOrderDialog.getWoPlannedStart().getText()
				+ "', '" + this.workOrderDialog.getWoPlannedEnd().getText()
				+ "', '" + this.workOrderDialog.getWoPlannedEffort().getText()
				+ "', '" + this.workOrderDialog.getWoDescription().getText()
				+ "', '" + "assigned', '"
				+ this.workOrderDialog.getWoNotes().getText() + "')";
		try {

			this.connection.insertquery(query);

		} catch (Exception e) {
			return false;
		}
		return true;
	}

}
